﻿using System;
using System.Collections.Generic;
using System.Xml;
using Orid.Management.Statistics.Tools.Decomposition.Model;
using Orid.Management.Common.Job;
using System.Globalization;

namespace Orid.Management.Statistics.Tools.Decomposition.Data
{
    class DataBase
    {
        /// <summary>
        /// return seconds
        /// </summary>
        /// <param name="downloadTime"></param>
        /// <param name="uploadTime"></param>
        /// <returns></returns>
        protected double GetCost(DateTime downloadTime, DateTime uploadTime)
        {

            TimeSpan ts1 = new TimeSpan(downloadTime.Ticks);
            TimeSpan ts2 = new TimeSpan(uploadTime.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();

            return ts.TotalSeconds;
        }

        protected int msTimeOut = 500;

        /// <summary>
        /// xml精简   方法修改  2010-12-20
        /// 
        /// xml精简   方法修改  2011-1-6
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        protected List<ModelProgress> GetProgress(string xml)
        {
            List<ModelProgress> listModelProgress = new List<ModelProgress>();


            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            XmlNode recoreNode = doc.DocumentElement.SelectSingleNode("./rd[@rt='1']");

            if (recoreNode != null)
            {

                for (int i = 0; i < recoreNode.ChildNodes.Count; i++)
                {
                    ModelProgress temp = new ModelProgress();
                    CultureInfo deCultureInfo = new CultureInfo("de-DE");


                    if (recoreNode.ChildNodes[i].Attributes["F"] != null)
                    {
                        string argFlow = recoreNode.ChildNodes[i].Attributes["F"].Value;
                        temp.ArgFlow = Convert.ToInt32(argFlow);
                    }

                    if (recoreNode.ChildNodes[i].Attributes["SD"] != null)
                    {
                        string argStartDatetime = recoreNode.ChildNodes[i].Attributes["SD"].Value;
                        temp.ArgStartDatetime = argStartDatetime;
                    }
                    if (recoreNode.ChildNodes[i].Attributes["FD"] != null)
                    {
                        string argFinishedDatetime = recoreNode.ChildNodes[i].Attributes["FD"].Value;
                        temp.ArgFinishedDatetime =  argFinishedDatetime;

                    }


                    listModelProgress.Add(temp);
                }
            }

            return listModelProgress;
            
        }

        protected DataProperty GetDataProperty(string xml)
        {
            DataProperty property = null;

            if ((xml != null) && (xml != string.Empty))
            {
                property = new Orid.Management.Common.Job.XmlSerializer<DataProperty>().XmlDeserialize(xml);
            } 

            return property;
        }

        #region property Field

        protected ModelProperty GetPropertyField(string propertyXml)
        {
            ModelProperty modelProperty = null;
            DataProperty property = new XmlSerializer<DataProperty>().XmlDeserialize(propertyXml);

            if (property != null)
            {
                modelProperty = new ModelProperty();

                modelProperty.WordFlag = property.WordFlag.ToString();
                modelProperty.WordDirection = property.WordDirection.ToString();
                modelProperty.ScriptType = property.ScriptType.ToString();
                modelProperty.WordType = property.WordType.ToString();
                modelProperty.ImageType = property.ImageType.ToString();
                modelProperty.MatterType = property.MatterType.ToString();
                modelProperty.Warning = property.Warning;
                modelProperty.UserOrder = ((Orid.Management.Component.UserOrders)property.UserOrder).ToString();
                modelProperty.IsConstraintEntry = property.IsConstraintEntry.ToString();
            }

            return modelProperty;
        }

        #endregion property Field
    }
}
